Page({
    data: {
        id: '', //上页传递的id
        name: '',
        nameErr: false,
        tel: '',
        telErr: false,
        area: '',
        areaErr: false,
        detail: '',
        detailErr: false,
        province: '',
        province_code: '',
        city: '',
        city_code: '',
        district: '',
        county_code: '',
        region: [],
        buttonLoading: false
    },
    timeId: '',
    onLoad: function() {
        wx.setNavigationBarTitle({
            title: '添加新地址'
        })
        const eventChannel = this.getOpenerEventChannel()
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        eventChannel.on('acceptDataFromOpenerPage', ({ addressInfo }) => {
            wx.setNavigationBarTitle({
                title: addressInfo.addr_id ? '修改地址' : '添加新地址'
            })
            this.setData({
                id: addressInfo.addr_id,
                name: addressInfo.contact,
                tel: addressInfo.mobile,
                area: addressInfo.province + addressInfo.city + addressInfo.district,
                detail: addressInfo.house,
                province: addressInfo.province,
                province_code: addressInfo.province_code,
                city: addressInfo.city,
                city_code: addressInfo.city_code,
                district: addressInfo.district,
                county_code: addressInfo.district_code,
                region: [addressInfo.province, addressInfo.city, addressInfo.district]
            })
        })
    },
    //选择地区
    cityChange({ detail }) {
        const [province, city, district] = detail.value
        const [province_code, city_code, county_code] = detail.code
        this.setData({
            province,
            province_code,
            city,
            city_code,
            district,
            county_code,
            area: province + city + district,
            region: detail.value
        })
    },
    focusInput() {
        this.setData({
            nameErr: false,
            telErr: false,
            areaErr: false,
            detailErr: false
        })
    },
    submit() {
        if (this.data.name === '') return this.setData({ nameErr: true })
        if (this.data.tel === '') return this.setData({ telErr: true })
        if (this.data.tel.length !== 11) {
            this.setData({ telErr: true })
            return wx.showToast({
                title: '手机号码输入有误',
                icon: 'none'
            })
        }
        //手机号验证正则
        // const telReg = new RegExp("^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$")
        // if (!telReg.test()) {
        //     this.setData({ telErr: true })
        //     return wx.showToast({
        //         title: '手机号码输入有误',
        //         icon: 'none'
        //     })
        // }
        if (this.data.area === '') return this.setData({ areaErr: true })
        if (this.data.detail === '') return this.setData({ detailErr: true })
        this.setData({ buttonLoading: true })
        let reqData = {
            contact: this.data.name,
            mobile: this.data.tel,
            province: this.data.province,
            city: this.data.city,
            district: this.data.district,
            province_code: this.data.province_code,
            city_code: this.data.city_code,
            district_code: this.data.county_code,
            addr: this.data.area,
            house: this.data.detail
        }
        if (this.data.id) reqData.addr_id = this.data.id
        this.request({
            url: this.data.id ? '/api/user/addr/update' : '/api/user/addr/add',
            method: 'POST',
            data: reqData
        }, res => {
            wx.showToast({
                title: this.data.id ? '修改成功' : '添加成功',
                icon: 'success',
            })
            this.timeId = setTimeout(() => {
                wx.navigateBack()
            }, 400)
        }, () => {
            this.setData({ buttonLoading: false })
        })
    },
    onUnload: function() {
        clearTimeout(this.timeId)
    },
})